package com.aloda.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.aloda.annotation.RateLimit;
import com.aloda.constant.ErrorCode;
import com.aloda.context.UserBaseContext;
import com.aloda.dto.user.UserSafetyDTO;
import com.aloda.dto.userSuggest.UserSuggestAddDTO;
import com.aloda.entity.UserSuggest;
import com.aloda.exception.WarnException;
import com.aloda.mapper.UserSuggestMapper;
import com.aloda.query.UserSuggestPageDTO;
import com.aloda.result.PageResult;
import com.aloda.service.UserSuggestService;
import com.aloda.vo.userSuggest.UserSuggestPageVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author aloda
 * @description 针对表【user_suggest(用户建议表)】的数据库操作Service实现
 * @createDate 2025-04-01 20:32:00
 */
@Service
public class UserSuggestServiceImpl extends ServiceImpl<UserSuggestMapper, UserSuggest> implements UserSuggestService {

    @Resource
    private UserSuggestMapper userSuggestMapper;

    /**
     * 分页查询
     *
     * @param userSuggestPageDTO
     * @return
     */
    @Override
    public PageResult<UserSuggestPageVO> pageQuery(UserSuggestPageDTO userSuggestPageDTO) {
        String contactType = userSuggestPageDTO.getContactType();
        String userAccount = userSuggestPageDTO.getUserAccount();
        String contact = userSuggestPageDTO.getContact();
        LocalDateTime endTime = userSuggestPageDTO.getEndTime();
        LocalDateTime beginTime = userSuggestPageDTO.getBeginTime();
        Page<UserSuggest> page = userSuggestPageDTO.toMpPageDefaultSortByCreateTimeDesc();
        LambdaQueryWrapper<UserSuggest> userSuggestLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userSuggestLambdaQueryWrapper.like(contactType != null, UserSuggest::getContactType, contactType)
                .like(userAccount != null, UserSuggest::getUserAccount, userAccount)
                .like(contact != null, UserSuggest::getContact, contact)
                .between(beginTime != null && endTime != null,
                        UserSuggest::getCreateTime, beginTime, endTime);
        userSuggestMapper.selectPage(page, userSuggestLambdaQueryWrapper);
        return PageResult.of(page, UserSuggestPageVO.class);
    }

    /**
     * 用户端新增建议
     *
     * @param userSuggestAddDTO
     */
    @Override
    @RateLimit
    public void add(UserSuggestAddDTO userSuggestAddDTO) {
        if (BeanUtil.isEmpty(userSuggestAddDTO)) {
            throw new WarnException(ErrorCode.PARAMS_ERROR);
        }
        UserSuggest userSuggest = new UserSuggest();
        UserSafetyDTO user = UserBaseContext.getUser();
        userSuggest.setUserId(user.getId());
        userSuggest.setContent(userSuggestAddDTO.getSuggestContent());
        userSuggest.setContactType(userSuggestAddDTO.getContactType());
        userSuggest.setType(userSuggestAddDTO.getSuggestType());
        userSuggest.setContact(userSuggestAddDTO.getContact());
        userSuggest.setUserAccount(user.getUserAccount());
        userSuggest.setCreateTime(LocalDateTime.now());
        userSuggest.setUpdateTime(LocalDateTime.now());
        userSuggestMapper.insert(userSuggest);
    }
}




